﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;
using System.Threading;
using AgileDashboard.Services;
using AgileDashboard.Entities;

namespace PlanningPokerMVC.Services
{
    public class WorkItemSyncHub : Hub
    {

        public void SyncWorkItems()
        {
            try
            {

                WorkItemService wiService = new WorkItemService();


                Clients.updateGaugeBar(10);
                Clients.displayMessage("Retrieving Work Items");

                Clients.updateGaugeBar(20);
                Clients.displayMessage("Still Retrieving Work Items");

                Guid guid = new Guid("c5326f9e-39b3-4d37-ae67-0d7076a967a8");
                List<WorkItem> workItems = wiService.GetWorkItems(guid, 499, false);

                Clients.displayMessage(String.Format("Retrieved {0} workitems from TFS", workItems.Count));
                Clients.updateGaugeBar(30);



                Clients.displayMessage(String.Format("Starting to save {0} workitems to the database", workItems.Count));

                wiService.SyncWorkItems(workItems);

                Clients.updateGaugeBar(100);

                Clients.displayMessage(String.Format("{0} workitems have been saved to the database", workItems.Count));
            }

            catch (Exception e)
            {

                Clients.displayMessage("An Exception occurred while syncing workitems");
                Clients.displayMessage(e.ToString());

            }
        }

    }
}